package cn.ejiakanghu.logistics.service.impl;

import cn.com.ejiakanghu.core.exceptions.BizException;
import cn.com.ejiakanghu.core.rabbitmq.DirectExchangeProducer;
import cn.ejiakanghu.logistics.service.GoodsChangeService;
import cn.hutool.core.util.StrUtil;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.util.*;
import org.linlinjava.litemall.db.vo.locks.PickUp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：sujp
 * @date ：Created in 2021/11/11 17:52
 * @description：换货业务的具体操作
 * @modified By：
 * @version: 1.0
 */
@Service
public class GoodsChangeServiceImpl extends LogisticBusiBaseServiceImpl implements GoodsChangeService {
    @Resource
    private LogisticsBaseServiceAbstractImpl logisticsBaseServiceAbstractImpl;
    @Resource
    private AgedmallLgMissionMapper agedmallLgMissionMapper;
    @Resource
    private AgedmallLgBaseInfoMapper agedmallLgBaseInfoMapper;


    /**
     * 换货操作  换货取货中 换货配货中 换货配送中 数据的新增 以及状态的修改
     *
     * @param manId
     * @param missionId
     * @param messageType takeOrder类型
     */
    @Override
    public void newInsertDataForReturns(Integer manId, Integer missionId, Integer messageType) {
        //更新数据
        AgedmallLgMission mission = agedmallLgMissionMapper.selectByPrimaryKey(missionId);
        AgedmallLgBaseInfo agedmallLgBaseInfo = agedmallLgBaseInfoMapper.selectByPrimaryKey(mission.getBaseInfoId().longValue());
        Integer stat = LgBaseInfoStatConstant.getNowStat(agedmallLgBaseInfo.getLgType(), messageType);
        agedmallLgBaseInfo.setLgStat(stat);
        agedmallLgBaseInfoMapper.updateByPrimaryKeySelective(agedmallLgBaseInfo);

        //新增agedmall_lg_receive_order  agedmall_lg_busi_record
        AgedmallLgReceiveOrder receiveOrder = insertReceiveOrder(agedmallLgBaseInfo, manId, 1, 1,mission.getId().longValue());
        insertBusiRecord(mission.getId().longValue(), receiveOrder.getId(), mission.getType(), agedmallLgBaseInfo.getOrderCode(), 1);

        //更改订单商品的状态
        //updateOrderGoodsStat(agedmallLgBaseInfo.getLgContent(), stat);
    }

    /**
     * 换货取货中
     *
     * @param manId
     * @param missionId
     * @param messageType takeOrder类型
     */
    @Override
    public void exchangeGoodsAndPickUp(Integer manId, Integer missionId, Integer messageType) {
        //receiveorder busireceive 新增  baseInfo状态修改
        newInsertDataForReturns(manId, missionId, messageType);

    }

    /**
     * 换货取货完成
     *
     * @param missionId
     * @param imgs
     */
    @Override
    public void exchangeAndPickupCompleted(Integer missionId, List<String> imgs) {
        //新增 busireceiveorder busiimg  baseInfo当前物流状态 业务是否结束更新   mission表的状态更新
        logisticsBaseServiceAbstractImpl.pickingCompleteOperation(missionId, imgs, LgBaseInfoStatConstant.CHANGE_BACKING_GOODS, LgBaseInfoStatConstant.CHANGE_BACK_GOODS_COMPLETE, BaseInfoLogisticsStatusErrorMessageEnum.NOT_IN_RETRIEVED_STATE.getMessage());

    }

    /**
     * 换货配货中
     *
     * @param manId
     * @param missionId
     * @param messageType takeOrder类型
     */
    @Override
    public void replacementAndDistribution(Integer manId, Integer missionId, Integer messageType) {
        newInsertDataForReturns(manId, missionId, messageType);
    }

    /**
     * 换货配货完成
     *
     * @param missionId
     * @param imgs
     */
    @Override
    public void replacementAndDistributionCompleted(Integer missionId, List<String> imgs) {
        logisticsBaseServiceAbstractImpl.pickingCompleteOperation(missionId, imgs, LgBaseInfoStatConstant.CHANGE_ALLOTING, LgBaseInfoStatConstant.CHANGE_ALLOT_COMPLETE, BaseInfoLogisticsStatusErrorMessageEnum.NOT_IN_STOCKING_STATE.getMessage());
    }

    /**
     * 换货配送中
     *
     * @param manId
     * @param missionId
     * @param messageType takeOrder类型
     */
    @Override
    public void exchangeGoodsDelivery(Integer manId, Integer missionId, Integer messageType) {
        newInsertDataForReturns(manId, missionId, messageType);
    }

    /**
     * 换货配送完成
     *
     * @param missionId
     */
    @Override
    public void exchangeGoodsDeliveryCompleted(Integer missionId) {
        logisticsBaseServiceAbstractImpl.deliveryCompletionOperation(missionId, LgBaseInfoStatConstant.CHANGE_SENDING, LgBaseInfoStatConstant.CHANGE_SEND_COMPLETE, BaseInfoLogisticsStatusErrorMessageEnum.NOTIN_DELIVERY_STATE.getMessage());

    }
}
